"use strict"

var base = "";

var sys = {};

sys.form = {};

/**
 * @param {FormElement} form 返回表单的参数
 * @return {URLSearchParams}
 */
sys.form.param = function(form){
  let params = new URLSearchParams();
  for(let e of form.elements){
    // console.info(e);
    if(e.name!=""&&e.disabled==false){
      switch(e.type){
        case "radio":
        case "checkbox":
          if(e.checked){
            // console.info(e.name+"="+e.value+"\t"+e.type);
            params.append(e.name,e.value);
          }
          break;
        case "select-multiple":
          for(let o of e.options){
            if(o.selected){
              // console.info(e.name+"="+o.value);
              params.append(e.name,o.value);
            }
          }
          break;
        default:
          // console.info(e.name+"="+e.value+"\t"+e.type);
          params.append(e.name,e.value);
      }
      
    }
  }
 
  return params;
}

sys.loading = {};

sys.loading.obj = null;

/**
 * 显示加载中
 */
sys.loading.show = function(){
  if(sys.loading.obj==null){
    sys.loading.obj = document.createElement("div");
    document.body.appendChild(sys.loading.obj);
    sys.loading.obj.innerHTML = `    <div style="position: fixed; z-index: 9999; width: 100vw; height: 100vh; left: 0; top: 0; display: flex; justify-content: center; align-items: center; background-color: rgba(0,0,0,0.1);">
      <svg style="width: 20vmin; height: 20vmin;" version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 38 38" preserveAspectRatio="xMinYMin meet">
        <text x="14" y="21" font-family="微软雅黑" font-size="4px"  style="letter-spacing:1.2; text-shadow: 0 0 2px #000000;" fill="#FFFFFF">加载中
          <animate attributeName="opacity" values="0;1;0" dur="1.8s" repeatCount="indefinite">
          </animate></text>
        <path fill="#373a42" d="M20,35c-8.271,0-15-6.729-15-15S11.729,5,20,5s15,6.729,15,15S28.271,35,20,35z M20,5.203
          C11.841,5.203,5.203,11.841,5.203,20c0,8.159,6.638,14.797,14.797,14.797S34.797,28.159,34.797,20
          C34.797,11.841,28.159,5.203,20,5.203z">
        </path>
      
        <path fill="#373a42" d="M20,33.125c-7.237,0-13.125-5.888-13.125-13.125S12.763,6.875,20,6.875S33.125,12.763,33.125,20
          S27.237,33.125,20,33.125z M20,7.078C12.875,7.078,7.078,12.875,7.078,20c0,7.125,5.797,12.922,12.922,12.922
          S32.922,27.125,32.922,20C32.922,12.875,27.125,7.078,20,7.078z">
        </path>
      
        <path fill="#2AA198" stroke="#2AA198" stroke-width="0.6027" stroke-miterlimit="10" d="M5.203,20
      			c0-8.159,6.638-14.797,14.797-14.797V5C11.729,5,5,11.729,5,20s6.729,15,15,15v-0.203C11.841,34.797,5.203,28.159,5.203,20z"
          transform="rotate(342.095 20 20)">
          <animateTransform attributeName="transform" type="rotate" from="0 20 20" to="360 20 20" calcMode="spline"
            keySplines="0.4, 0, 0.2, 1" keyTimes="0;1" dur="2s" repeatCount="indefinite"></animateTransform>
        </path>
      
        <path fill="#859900" stroke="#859900" stroke-width="0.2027" stroke-miterlimit="10" d="M7.078,20
        c0-7.125,5.797-12.922,12.922-12.922V6.875C12.763,6.875,6.875,12.763,6.875,20S12.763,33.125,20,33.125v-0.203
        C12.875,32.922,7.078,27.125,7.078,20z"
          transform="rotate(130.713 20 20)">
          <animateTransform attributeName="transform" type="rotate" from="0 20 20" to="360 20 20" dur="1.8s"
            repeatCount="indefinite"></animateTransform>
        </path>
      </svg>
    </div>`;
  }else{
    sys.loading.obj.style.display="block";
  }
}

/**
 * 隐藏加载中
 */
sys.loading.hide = function(){
  if(sys.loading.obj!=null){
    sys.loading.obj.style.display="none";
  }
}


/**
 * 提示对话框
 * @param {string} msg 提示消息
 * @param {string} title 对话框标题 默认： "系统提示"
 * @param {function} 窗口关闭回调函数  
 */
sys.alert = function(msg,title="系统提示",fn=function(){}){
  $(`<div class="modal fade" data-backdrop="static" tabindex="-1">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">${title}</h5>
          <button type="button" class="close" data-dismiss="modal">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ${msg}
        </div>
      </div>
    </div>
  </div>`).appendTo("body").on("hidden.bs.modal",function(){
      $(this).remove();
      fn();
    }).modal("show");
}


/**
 * 确认提示
 * @param {string} msg
 * @param {function} fn 确认回调函数，函数参数ret:true|false 
 * @param {string} title 默认："确认提示" 
 */
sys.confirm = function(msg,fn=function(ret){},title="确认提示"){
  $(`<div class="modal fade" data-backdrop="static" tabindex="-1">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">${title}</h5>
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ${msg}
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary _btn_action" data-ret="true" data-dismiss="modal">确定</button>
            <button type="button" class="btn btn-secondary _btn_action" data-ret="false" data-dismiss="modal">取消</button>
          </div>
        </div>
      </div>
    </div>`).appendTo("body").on("hidden.bs.modal",function(){
      $(this).remove();
    }).on("click","._btn_action",function(){
        fn($(this).data("ret"));
      }).modal("show");
}

/**
 * 输入提示
 * @param {string} msg
 * @param {function} 输入回调函数 
 * @param {string} title 默认："输入提示" 
 * @param {string} defaultValue 默认：""
 */
sys.prompt = function(msg,fn=function(content){},title="输入提示",defaultValue=""){
  $(`<div class="modal fade" data-backdrop="static" tabindex="-1">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">${title}</h5>
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text">${msg}</span>
              </div>
              <input type="text" class="form-control _input" value="${defaultValue}">
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary _btn_action" data-dismiss="modal">确定</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
          </div>
        </div>
      </div>
    </div>`).appendTo("body").on("hidden.bs.modal",function(){
      $(this).remove();
    }).on("click","._btn_action",function(){
        fn($(this).parents(".modal-dialog:eq(0)").find("input._input").val());
      }).modal("show");
}

sys.toastr = function(option){
  GrowlNotification.notify($.extend({
    title: "系统提示",
    description: '提示内容',
    type: "default",
    closeTimeout: 5000,
    image: {
      visible: true
    },
    position: "bottom-right",
    closeWith: ['click'],
    animation: {
      open: 'slide-in',
      close: 'slide-out'
    },
    showButtons: false,
    showProgress: true
  },option));
  
};

/**
 * 系统通知
 * @param {string} msg 内容
 * @param {string} title 标题 默认： "系统通知" 
 * @param {number} timeout 自动隐藏时间，默认：5000毫秒 
 */
sys.toastr.alert= function(msg,title="系统通知",timeout=5000){
  sys.toastr({
    title: title,
    description: msg,
    type: "default",
    closeTimeout: timeout    
  });
}


/**
 * 成功提示
 * @param {string} msg 内容
 * @param {string} title 标题 默认： "成功提示" 
 * @param {number} timeout 自动隐藏时间，默认：5000毫秒 
 */
sys.toastr.success = function(msg,title="成功提示",timeout=5000){
  sys.toastr({
    title: title,
    description: msg,
    type: "success",
    closeTimeout: timeout    
  });
}


/**
 * 消息提示
 * @param {string} msg 内容
 * @param {string} title 标题 默认： "消息提示" 
 * @param {number} timeout 自动隐藏时间，默认：5000毫秒 
 */
sys.toastr.info = function(msg,title="消息提示",timeout=5000){
  sys.toastr({
    title: title,
    description: msg,
    type: "info",
    closeTimeout: timeout    
  });
}

/**
 * 警告提示
 * @param {string} msg 内容
 * @param {string} title 标题 默认： "警告提示" 
 * @param {number} timeout 自动隐藏时间，默认：5000毫秒 
 */
sys.toastr.warn = function(msg,title="警告提示",timeout=5000){
  sys.toastr({
    title: title,
    description: msg,
    type: "warning",
    closeTimeout: timeout    
  });
}


/**
 * 错误提示
 * @param {string} msg 内容
 * @param {string} title 标题 默认： "错误提示" 
 * @param {number} timeout 自动隐藏时间，默认：5000毫秒 
 */
sys.toastr.error = function(msg,title="错误提示",timeout=5000){
  sys.toastr({
    title: title,
    description: msg,
    type: "error",
    closeTimeout: timeout    
  });
}


jQuery.ajaxSetup({
  beforeSend:function(xhr){
    sys.loading.show();
  },
  complete:function(XMLHttpRequest, textStatus) {
	  sys.loading.hide();
  }
});

sys.list = function(config={}){
  config = $.extend({
    show:null,//需要显示的列表，内部是显示的模板
    type:"get",//ajax的请求方式：get|post
    url:"",//请求地址
    format:function(item,data){}//格式化处理函数
  },config);
  let dataShow1 = $(config.show);
  let template = dataShow1.children().remove();

  
  $[config.type](
    base+config.url,
    function(data){
      if(data.code==200){
        for(let i=0;i<data.data.length;i++){
          let obj = data.data[i];
          obj.rowNum=i+1;
          let item = template.clone();
          $.each(obj,function(name,value){
            item.find("._"+name).text(value);
          });
          item.data("row",obj);
          
          config.format(item,obj);
          
          dataShow1.append(item);
        }
      }else{
        sys.toastr.error(data.message);
      }
    
    },"json"
  );
}

$(function(){
	$("[data-load]").each(function(){
		$(this).load($(this).data("load"));
	});
	
	$("body").on("click","._exit",function(){
		sys.confirm("您确定要退出登录吗?",function(r){
			if(r){
				window.location="logout.let";
			}
		},"退出确认");
	});
});